package com.bargetor.cubilose

import cn.jiguang.common.ClientConfig
import cn.jpush.api.JPushClient
import cn.jpush.api.push.model.PushPayload
import org.springframework.stereotype.Component
import sun.security.krb5.internal.Krb5.getErrorMessage
import cn.jiguang.common.resp.APIRequestException
import cn.jiguang.common.resp.APIConnectionException
import cn.jpush.api.push.PushResult
import org.apache.log4j.Logger


@Component
class CubiloseJPushHelper{
    companion object {
        private const val appKey = "4b473363de2044d8cbe5ea43"
        private const val masterSecret = "98d39cec8f718a4543c0bb88"
        val jPushClient = JPushClient(masterSecret, appKey)
        val logger = Logger.getLogger(CubiloseJPushHelper::class.java)!!
    }

    fun push(payload: PushPayload){
        try {
            val result = jPushClient.sendPush(payload)
            logger.info("Got result - " + result)
        } catch (e: APIConnectionException) {
            // Connection error, should retry later
            logger.error("Connection error, should retry later", e)

        } catch (e: APIRequestException) {
            // Should review the error, and fix the request
            logger.error("Should review the error, and fix the request", e)
            logger.info("HTTP Status: " + e.status)
            logger.info("Error Code: " + e.errorCode)
            logger.info("Error Message: " + e.errorMessage)
        }

    }
}